<?php
/*
 * @Author: KingMin 
 * @Date: 2021-07-13 22:34:59 
 * @Last Modified by:   KingMin 
 * @Last Modified time: 2021-07-13 22:34:59 
 * @The authors website: https://www.kingmin.cn  
 */
/**
 * 日志查询
 * @copyright (c) KINGMIN All Rights Reserved
 */

class Log_Model {

    private $db;

    function __construct() {
        $this->db = Database::getInstance();
    }
    // 获取登录日志列表
    function getLoginLog($pages,$dt){
        @$isAll=$pages['isAll'];
        $output = [
			'pages'=>[
			    'isAll'=>$isAll?true:false,
    			'recordCount' => 0,     //满足条件的总记录数
    		    'pageSize' => 10,        //每页大小，即每页最多可以显示的记录数量
    		    'pageCount' => 0,       //总页数
    		    'pno' => 1,          //当前数据所在页号
			 ],
			'list' => [] ,         //当前页中的数据
	    ];
	    $isfym="";$uSql="";$ipSql="";$stSql="";
	    if(@$pages['pno']){
		  $output['pages']['pno'] = intval($pages['pno']);
		}
		if(@$pages['pageSize']){
		  $output['pages']['pageSize']= intval($pages['pageSize']);
		}
	    $pno = $output['pages']['pno'];$pgs =$output['pages']['pageSize'];
	    if(@$dt['uid']||@$dt['uid']=='0'){
	        $uSql=" AND `uid`='".$dt['uid']."' ";
	    }
	    if(@$dt['uname']){
	        $uSql=" AND `user_name`='".$dt['uname']."' ";
	    }
	    if(@$dt['ip']){
	        $ipSql=" AND `log_ip`='".$dt['ip']."' ";
	    }
	    if(@$dt['log_status']){
	        $stSql=" AND `log_status`='".$dt['log_status']."' ";
	    }
	    if(@$dt['status']){
	        $stSql=" AND `status`='".$dt['status']."' ";
	    }
	    $totalSql = " SELECT COUNT(*) as total  FROM " . DB_PREFIX . "sys_log_login WHERE `isdel`='0' $uSql $ipSql $stSql ;";
		$res = $this->db->fetch_array($this->db->query($totalSql));
		$output['pages']['recordCount'] = intval($res['total']);
		//计算总页数
		$output['pages']['pageCount'] = ceil($output['pages']['recordCount']/$output['pages']['pageSize']);
		//获取指定页中的数据
		$start = ($output['pages']['pno']-1)*$output['pages']['pageSize'];
		$count = $output['pages']['pageSize'];
		if(!$output['pages']['isAll']){
			$isfym="LIMIT $start,$count";
	    }
	    
	    $sql = "SELECT * FROM " . DB_PREFIX . "sys_log_login WHERE `isdel`='0' $uSql $ipSql $stSql ORDER BY id DESC  $isfym;";
        $logs = $this->db->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            unset($v['user_password']);
            $v['user']=LoginAuth::getLoginDetail($v['uid']);
            if($v['uid']=='0'){
                $v['user']=[
                        "uid"=>0,
                        "uname"=>"系统用户",
                    ];
            }
            if($v['status']=='admin'){
                $tag=[
                    "type"=>"info",
                    "label"=>"管理后台",
                ];
            }
            if($v['status']=='wapp'){
                $tag=[
                    "type"=>"success",
                    "label"=>"小程序",
                ];
            }
            if($v['status']=='h5'){
                $tag=[
                    "type"=>"warning",
                    "label"=>"H5",
                ];
            }
            if($v['status']=='app'){
                $tag=[
                    "type"=>"warning",
                    "label"=>"APP",
                ];
            }
            $v['status']=$tag;
            if($v['log_status']=='1'){
                $tag=[
                    "type"=>"success",
                    "label"=>"登录成功",
                ];
            }
            if($v['log_status']=='2'){
                $tag=[
                    "type"=>"danger",
                    "label"=>"账号错误",
                ];
            }
            if($v['log_status']=='3'){
                $tag=[
                    "type"=>"danger",
                    "label"=>"密码错误",
                ];
            }
            if($v['log_status']=='5'){
                $tag=[
                    "type"=>"warning",
                    "label"=>"登录异常",
                ];
            }
            $v['log_status']=$tag;
            $data[]=$v;
        }
        $output['list']=$data;
        return $output;
    }
    // 获取操作日志列表
    function getOperationLog($pages=[],$dt=[]){
        @$isAll=$pages['isAll'];
        $output = [
			'pages'=>[
			    'isAll'=>$isAll?true:false,
    			'recordCount' => 0,     //满足条件的总记录数
    		    'pageSize' => 10,        //每页大小，即每页最多可以显示的记录数量
    		    'pageCount' => 0,       //总页数
    		    'pno' => 1,          //当前数据所在页号
			 ],
			'list' => [] ,         //当前页中的数据
	    ];
	    $isfym="";$uSql="";$ipSql="";$stSql="";$tySql="";
	    if(@$pages['pno']){
		  $output['pages']['pno'] = intval($pages['pno']);
		}
		if(@$pages['pageSize']){
		  $output['pages']['pageSize']= intval($pages['pageSize']);
		}
	    $pno = $output['pages']['pno'];$pgs =$output['pages']['pageSize'];
	    if(@$dt['uid']||@$dt['uid']=='0'){
	        $uSql=" AND `uid`='".$dt['uid']."' ";
	    }
	    if(@$dt['ip']){
	        $ipSql=" AND `log_ip`='".$dt['ip']."' ";
	    }
	    
	    if(@$dt['log_type']){
	        $tySql=" AND `log_type`='".$dt['log_type']."' ";
	    }
	    if(@$dt['key']){
	        $tySql.=" AND ( `log_title` LIKE '%".$dt['key']."%' OR `log_msg` LIKE '%".$dt['key']."%'  OR `log_data` LIKE '%".$dt['key']."%'  OR `json_result` LIKE '%".$dt['key']."%' OR `remark` LIKE '%".$dt['key']."%' ) ";
	    }
	    if(@$dt['minDate']){
	        $tySql.=" AND `create_time`>='".$dt['minDate']."' ";
	    }
	    if(@$dt['maxDate']){
	        $tySql.=" AND `create_time`<='".$dt['maxDate']."' ";
	    }
	    if(@$dt['operator_type']||@$dt['operator_type']=='0'){
	        $tySql.=" AND `operator_type`='".$dt['operator_type']."' ";
	    }
	    if(@$dt['business_type']||@$dt['business_type']=='0'){
	        $tySql.=" AND `business_type`='".$dt['business_type']."' ";
	    }
	    if(@$dt['status']||@$dt['status']=='0'){
	        $tySql.=" AND `status`='".$dt['status']."' ";
	    }
	    $totalSql = " SELECT COUNT(*) as total  FROM " . DB_PREFIX . "sys_log_operation WHERE `isdel`='0' $uSql $ipSql $stSql $tySql ;";
		$res = $this->db->fetch_array($this->db->query($totalSql));
		$output['pages']['recordCount'] = intval($res['total']);
		//计算总页数
		$output['pages']['pageCount'] = ceil($output['pages']['recordCount']/$output['pages']['pageSize']);
		//获取指定页中的数据
		$start = ($output['pages']['pno']-1)*$output['pages']['pageSize'];
		$count = $output['pages']['pageSize'];
		if(!$output['pages']['isAll']){
			$isfym="LIMIT $start,$count";
	    }
	    
	    $sql = "SELECT * FROM " . DB_PREFIX . "sys_log_operation WHERE `isdel`='0' $uSql $ipSql $stSql $tySql ORDER BY id DESC  $isfym;";
        $logs = $this->db->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            // unset($v['user_password']);
            $v['user']=LoginAuth::getLoginDetail($v['uid']);
            if($v['uid']=='0'){
                $v['user']=[
                        "uid"=>0,
                        "uname"=>"系统用户",
                    ];
            }
            
            $v['log_data']=htmlspecialchars_decode($v['log_data']);
            $v['log_data_json']=json_decode($v['log_data']);
            $v['json_result']=htmlspecialchars_decode($v['json_result']);
            $v['json_result_json']=json_decode($v['json_result']);
            if($v['business_type']=='0'){
                $tag=[
                    "type"=>"info",
                    "label"=>"其他",
                ];
            }
            if($v['business_type']=='1'){
                $tag=[
                    "type"=>"success",
                    "label"=>"新增",
                ];
            }
            if($v['business_type']=='2'){
                $tag=[
                    "type"=>"warning",
                    "label"=>"修改",
                ];
            }
            if($v['business_type']=='3'){
                $tag=[
                    "type"=>"danger",
                    "label"=>"删除",
                ];
            }
            $v['business_type']=$tag;
            if($v['status']=='0'){
                $tag=[
                    "type"=>"success",
                    "label"=>"正常",
                ];
            }
            if($v['status']=='1'){
                $tag=[
                    "type"=>"warning",
                    "label"=>"异常",
                ];
            }
            $v['status']=$tag;
            if($v['operator_type']=='admin'){
                $v['operator_type']="管理后台";
            }elseif($v['operator_type']=='wapp'){
                $v['operator_type']="小程序";
            }elseif($v['operator_type']=='h5'){
                $v['operator_type']="H5";
            }elseif($v['operator_type']=='app'){
                $v['operator_type']="APP";
            }else {
                $v['operator_type']="其他";
            }
            $data[]=$v;
        }
        $output['list']=$data;
        return $output;
    }
    // 获取错误日志列表
    function getErrorLog($pages=[],$dt=[]){
        @$isAll=$pages['isAll'];
        $output = [
			'pages'=>[
			    'isAll'=>$isAll?true:false,
    			'recordCount' => 0,     //满足条件的总记录数
    		    'pageSize' => 10,        //每页大小，即每页最多可以显示的记录数量
    		    'pageCount' => 0,       //总页数
    		    'pno' => 1,          //当前数据所在页号
			 ],
			'list' => [] ,         //当前页中的数据
	    ];
	    $isfym="";$uSql="";$ipSql="";$stSql="";$tySql="";
	    if(@$pages['pno']){
		  $output['pages']['pno'] = intval($pages['pno']);
		}
		if(@$pages['pageSize']){
		  $output['pages']['pageSize']= intval($pages['pageSize']);
		}
	    $pno = $output['pages']['pno'];$pgs =$output['pages']['pageSize'];
	    if(@$dt['uid']||@$dt['uid']=='0'){
	        $uSql=" AND `uid`='".$dt['uid']."' ";
	    }
	    if(@$dt['ip']){
	        $ipSql=" AND `log_ip`='".$dt['ip']."' ";
	    }
	    if(@$dt['key']){
	        $tySql.=" AND ( `log_data` LIKE '%".$dt['key']."%' OR `log_msg` LIKE '%".$dt['key']."%' ) ";
	    }
	    if(@$dt['minDate']){
	        $tySql.=" AND `create_time`>='".$dt['minDate']."' ";
	    }
	    if(@$dt['maxDate']){
	        $tySql.=" AND `create_time`<='".$dt['maxDate']."' ";
	    }
	    if(@$dt['operator_type']||@$dt['operator_type']=='0'){
	        $tySql.=" AND `operator_type`='".$dt['operator_type']."' ";
	    }
	    if(@$dt['error_type']||@$dt['error_type']=='0'){
	        $tySql.=" AND `error_type`='".$dt['error_type']."' ";
	    }
	    $totalSql = " SELECT COUNT(*) as total  FROM " . DB_PREFIX . "sys_log_error WHERE `isdel`='0' $uSql $ipSql $stSql $tySql ;";
		$res = $this->db->fetch_array($this->db->query($totalSql));
		$output['pages']['recordCount'] = intval($res['total']);
		//计算总页数
		$output['pages']['pageCount'] = ceil($output['pages']['recordCount']/$output['pages']['pageSize']);
		//获取指定页中的数据
		$start = ($output['pages']['pno']-1)*$output['pages']['pageSize'];
		$count = $output['pages']['pageSize'];
		if(!$output['pages']['isAll']){
			$isfym="LIMIT $start,$count";
	    }
	    
	    $sql = "SELECT * FROM " . DB_PREFIX . "sys_log_error WHERE `isdel`='0' $uSql $ipSql $stSql $tySql ORDER BY id DESC  $isfym;";
        $logs = $this->db->query($sql);
        // $output['sql']=$sql;
        $data = array();
        foreach ($logs as $key => $v) {
            // unset($v['user_password']);
            $v['log_data']=htmlspecialchars_decode($v['log_data']);
            $v['log_data_json']=json_decode($v['log_data']);
            $v['json_result']=htmlspecialchars_decode($v['json_result']);
            $v['json_result_json']=json_decode($v['json_result']);
            if($v['error_type']=='0'){
                $tag=[
                    "type"=>"info",
                    "label"=>"其他",
                ];
            }elseif($v['error_type']=='1'){
                $tag=[
                    "type"=>"danger",
                    "label"=>"数据库",
                ];
            }else{
                $tag=[
                    "type"=>"",
                    "label"=>"NOT",
                ];
            }
            $v['error_type']=$tag;
            if($v['operator_type']=='admin'){
                $v['operator_type']="管理后台";
            }elseif($v['operator_type']=='wapp'){
                $v['operator_type']="小程序";
            }elseif($v['operator_type']=='h5'){
                $v['operator_type']="H5";
            }elseif($v['operator_type']=='app'){
                $v['operator_type']="APP";
            }else {
                $v['operator_type']="其他";
            }
            $data[]=$v;
        }
        $output['list']=$data;
        return $output;
    }
}
